// License: http://www.codeplex.com/entlibcontrib/Project/License.aspx
// Author: Randy

using System;
using Microsoft.Practices.EnterpriseLibrary.Logging.Configuration;

namespace EntLibContrib.Logging.Configuration
{
    /// <summary>
    /// Container for the file properties when the class does not implement <see cref="IFileTraceListenerData"/>.
    /// </summary>
    public class FileTraceListenerData : IFileTraceListenerData
    {
        #region IFileTraceListenerData Members
        private string dataFileName;
        private string dataFooter;
        private string dataFormatter;
        private string dataHeader;
        private string dataName;

        /// <summary>
        /// File name to write the log entry into.
        /// </summary>
        public string FileName
        {
            get { return dataFileName; }
        }

        /// <summary>
        /// Footer to write after the log entry.
        /// </summary>
        public string Footer
        {
            get { return dataFooter; }
        }

        /// <summary>
        /// Name of the formatter that will format the log entry.
        /// </summary>
        public string Formatter
        {
            get { return dataFormatter; }
        }

        /// <summary>
        /// Header to write before the log entry.
        /// </summary>
        public string Header
        {
            get { return dataHeader; }
        }

        /// <summary>
        /// Name of the trace listener instance.
        /// </summary>
        public string Name
        {
            get { return dataName; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor to initialize a new instance of the class.
        /// </summary>
        /// <param name="traceData"></param>
        public FileTraceListenerData(FlatFileTraceListenerData traceData)
        {
            dataFileName = traceData.FileName;
            dataFooter = traceData.Footer;
            dataFormatter = traceData.Formatter;
            dataHeader = traceData.Header;
            dataName = traceData.Name;
        }

        /// <summary>
        /// Constructor to initialize a new instance of the class.
        /// </summary>
        /// <param name="traceData"></param>
        public FileTraceListenerData(RollingFlatFileTraceListenerData traceData)
        {
            dataFileName = traceData.FileName;
            dataFooter = traceData.Footer;
            dataFormatter = traceData.Formatter;
            dataHeader = traceData.Header;
            dataName = traceData.Name;
        }

        #endregion

        #region GetFileData

        private static void AsFlatFileTraceListenerData(TraceListenerData traceData, ref IFileTraceListenerData fileData)
        {
            if (fileData == null)
            {
                FlatFileTraceListenerData fileFlat = traceData as FlatFileTraceListenerData;
                if (fileFlat != null)
                    fileData = new FileTraceListenerData(fileFlat);
            }
        }
        private static void AsRollingFlatFileTraceListenerData(TraceListenerData traceData, ref IFileTraceListenerData fileData)
        {
            if (fileData == null)
            {
                RollingFlatFileTraceListenerData fileFlat = traceData as RollingFlatFileTraceListenerData;
                if (fileFlat != null)
                    fileData = new FileTraceListenerData(fileFlat);
            }
        }

        /// <summary>
        /// Get common file properties if the derived class has them.
        /// </summary>
        /// <param name="traceData"></param>
        /// <returns>common file properties</returns>
        public static IFileTraceListenerData GetFileData(TraceListenerData traceData)
        {
            IFileTraceListenerData fileData = traceData as IFileTraceListenerData;
            AsFlatFileTraceListenerData(traceData, ref fileData);
            AsRollingFlatFileTraceListenerData(traceData, ref fileData);
            return fileData;
        }

        #endregion
    }
}
